package com.yifi.system.controller;

import com.jfinal.aop.Clear;
import com.jfinal.core.Path;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.ehcache.CacheKit;
import com.yifi.common.base.BaseController;
import com.yifi.common.vo.TableDataInfo;
import net.sf.ehcache.CacheManager;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 姚善利
 * @classname com.yifi.system.controller
 * @description 缓存历史
 * @date 2022/11/19 10:37
 */
@Path(value = "ehcache_list",viewPath = "/system/")
public class ehcache_list_controller extends BaseController {

    public void index()
    {
        render("ehcache_list.html");
    }

    public void list()
    {
        List<Record> list=new ArrayList<Record>();
        //List<Object> listObj=CacheKit.get("bigscreen","getData_crk");
        //List<Object> listCah= CacheKit.getKeys("/getData_crk");
        CacheManager cacheManager = CacheKit.getCacheManager();
        String[] keys = cacheManager.getCacheNames();
        for (String str:keys) {
            Record record=new Record();
            record.set("id",str);
            List listca = CacheKit.getKeys(str);
            record.set("value",listca.size());
            list.add(record);
        }
        TableDataInfo data=new TableDataInfo();
        data.setRows(list);
        data.setCode(0);
        data.setTotal(0);
        data.setTotalPage(0);
        renderJson(data);
    }

    public void remove()
    {
        String ids=getPara("ids","");
        String[] idArr=ids.split(",");
        for (String key:idArr) {
            CacheKit.removeAll(key);
            CacheKit.getCacheManager().removeCache(key);
        }
        renderJson(success("删除成功！"));
    }

    @Clear
    public void clearAll()
    {
        CacheKit.getCacheManager().clearAll();
        CacheKit.getCacheManager().removalAll();
        renderJson(success("清除成功！"));
    }
}
